/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tablasSql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author FERRETERIA
 */
public class Familias {
    
    public static Object [][] getFamilias (String familia) {
        
        String [] colName = {"id","codigo", "Descripcion"};
        Class [] tipos = {Integer.class,Integer.class, String.class}; 
        String sql = "select id, codigo, descripcion from nFamilias";
        String tabla = "nFamilias";
        if (familia.compareTo("") != 0) {
            tabla += " where descripcion like '%" + familia + "%'" ;
            sql += " where descripcion like '%" + familia + "%'" ;
        }
        sql += " order by descripcion asc";
        return new Sql().GetTabla(colName, tabla, sql, tipos);
    }
    
    
    public static String getNombreFamilia (String cod) {
        String familia = "Ninguno";
        try {
            String sql = "Select * from nFamilias where id = '" + cod + "'";
            ResultSet res = new Sql().getDatos(sql);
            res.next();
            familia = res.getString("Descripcion");
        } catch (SQLException ex) {
            System.out.println(ex);
        }
        return familia;
    }
    
    public static int getIdFamilia (String name) {
        int id = -1;
        try {
            String sql = "Select * from nFamilias where descripcion like '" + name + "'";
            ResultSet res = new Sql().getDatos(sql);
            res.next();
            id = res.getInt("id");
        } catch (SQLException ex) {
            return -1;
        }
        return id;
        
    }
    
    public static void guardar (String [] datos, boolean guardar) {
        String sql = "";
        int numString = 3;
        int numFloat = 0;
        if (!guardar) {
           sql = "update nfamilias set descripcion = ?, codigo = ? where id = ?";
        }
        else {
            sql = "insert into nfamilias (id, codigo, descripcion) values (?,?,?)";
        }
        new Sql().actualizarDatos(sql, datos, numString, numFloat);
    }
    
    public static void borrar(String id) {
        String sql = "delete from nfamilias where id = " + id;
        new Sql().hacerConsulta(sql);
    }
    
}
